/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package quanlysieuthi;

import DAO.CUserGroupDAO;
import DAO.SubConfig;
import DAO.UserDAO;
import DTO.CFields;
import DTO.CUser_group;
import DTO.User;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import Model.CModel;

public class FrmUser extends javax.swing.JFrame {

    /**
     * Creates new form FrmUser
     */
    public FrmUser() {
        initComponents();
        currentIndex = 0;
        this.loadListUsers();
    }

    private Boolean add = false;
    private Boolean edit = false;
    private Boolean delete = false;
    int currentIndex = 0;
    int numberRow = 0;
    private void setEditable(Boolean b){
        this.txtuser_name.setEditable(b);
        this.txtpass_word.setEditable(b);
    }
    List<User> lUser = new ArrayList<User>();
    UserDAO userDAO = new UserDAO();
    SubConfig config = null;
    User currentUser = null;
    List<CUser_group> luserGroup = new ArrayList<CUser_group>();
    CUserGroupDAO usergroupDAO = new CUserGroupDAO();
    String ho_tennv ="";
    long id = -1;
    //load combobox
    //load danh sach user   
    private void loadListUsers(){
        config = SubConfig.getInstance();
        config.init();
       // userDAO.sWhere = " where active = true";
       
        //user_group
        usergroupDAO.sWhere = " where id <> 1 ";
        this.luserGroup = this.usergroupDAO.getAll(config);
        this.cbbgroup_id.removeAllItems();
        config.close();//dong co so du lieu
        
        //load combobox
        for(CUser_group _item : luserGroup){
            this.cbbgroup_id.addItem(_item.getTenGroup());
        }
        this.cbbgroup_id.setSelectedIndex(1);
        //load table
       loadDataUsers();
       
    }
    
    private void loadDataUsers(){
        config = SubConfig.getInstance();
        config.init();
        userDAO.sWhere = " where group_id <> 1";
        lUser = userDAO.getAll(config);
        config.close();//dong co so du lieu
        Vector<Vector> data = new Vector<Vector>();
        for(User item : lUser){
            Vector<String> row = new Vector<String>();
            row.addElement(item.getUserName());
            row.addElement("" + item.getGroupUser());
            data.add(row);
        }
        Vector<String> columnNames = new Vector<String>();
        columnNames.addElement("Tai khoan");
        columnNames.addElement("Nhom");
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        this.tbldanh_sach.removeAll();
        this.tbldanh_sach.setModel(model);
        this.tbldanh_sach.setFillsViewportHeight(true);
        this.setEnabled(true);
        numberRow = lUser.size();        
        this.view(currentIndex);
        this.isSetEnabled(true);
    }
    
    private void view(int index){
        id = -1;
        currentUser = null;
        if(index >=0 && index < numberRow){
            User item = this.lUser.get(index);           
            CModel _model = new CModel(QuanLySieuThi.user_name);            
            currentUser = item;
            this.txtpass_word.setText(item.getPassWord());
            this.txtre_pass_word.setText(item.getPassWord());
            this.txtuser_name.setText(item.getUserName());
            this.chkactive.setSelected(item.getActive());
            this.txthoten.setText(item.getHoTen());
            this.txtRows.setText(String.format("%s/%s",this.currentIndex + 1,this.numberRow));
            int i = _model.indexUserGroup(luserGroup, item.getGroupUser());
            System.out.println(String.format("%s id =%s", i,item.getGroupUser()));
            this.cbbgroup_id.setSelectedIndex(i);
            this.cbbgroup_id.setEnabled(false);
            this.tbldanh_sach.setRowSelectionInterval(index, index);
            id =  item.getId();//cbbgroup_id
        }
    }
    //xoa text o control
    private void clearText(){
        this.txtpass_word.setText("");
        this.txtuser_name.setText("");
        this.txtre_pass_word.setText("");
        this.txthoten.setText("");
    }
    
    private Boolean isEmpty(){
        if(txtuser_name.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this, "Khong duoc bo trong ten tai khoan");            
           return true; 
        }
        if(txtpass_word.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this, "Khong duoc bo trong mat khau");            
           return true; 
        }
        if(!txtpass_word.getText().equals(this.txtre_pass_word.getText())){
            JOptionPane.showMessageDialog(this, "Mat khau khong trung khop");            
           return true; 
        }
        if(txthoten.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this, "Khong duoc bo trong nhan vien");            
           return true; 
        }
        
        return false;
    }
    private long userId = -1;
    private Boolean addNew(){
        //can viet ham kiem tra dau vao
        if(isEmpty()){        
            return false;
        }
        config = SubConfig.getInstance();
        config.init();
        UserDAO usDAO = new UserDAO();
        
        String user_name = this.txtuser_name.getText();
        String pass_word = this.txtpass_word.getText();
        Boolean flag = usDAO.Exists("user_name", user_name, CFields.tb_user, config);
        if(flag){
            JOptionPane.showMessageDialog(this, String.format("Tai khoan %s nay da ton tai.", user_name));
            return false;
        }
        int group_id = 1;
        int index = this.cbbgroup_id.getSelectedIndex();
        if(index >= 0 && index < luserGroup.size()){
            CUser_group group = luserGroup.get(index);
            group_id = (int)group.getId();
        }
        Boolean active = this.chkactive.isSelected();
        ho_tennv = this.txthoten.getText();
        CModel _model = new CModel(QuanLySieuThi.user_name);
        userId = (long)_model.insertUser(user_name, pass_word, group_id, active);
        System.out.println("user id : " + userId);
        _model.saveHisTory();
        _model.Close();        
        loadDataUsers();
        return true;
    }
    private Boolean editRow(){
        if(isEmpty()){        
            return false;
        }
        String user_name = this.txtuser_name.getText();
        String pass_word = this.txtpass_word.getText();
        int group_id = 1;
        int index = this.cbbgroup_id.getSelectedIndex();
        if(index >= 0 && index < luserGroup.size()){
            CUser_group group = luserGroup.get(index);
            group_id = (int)group.getId();
        }
        Boolean active = this.chkactive.isSelected();
        CModel _model = new CModel(QuanLySieuThi.user_name);                
        _model.updateUser(user_name, pass_word, group_id, active,id);
        _model.saveHisTory();
        _model.Close();
        loadDataUsers();
        return true;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnedit = new javax.swing.JButton();
        btnsave = new javax.swing.JButton();
        btnadd = new javax.swing.JButton();
        btnclose = new javax.swing.JButton();
        btndelete = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        txtre_pass_word = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        cbbgroup_id = new javax.swing.JComboBox();
        chkactive = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        txtuser_name = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txthoten = new javax.swing.JTextField();
        txtpass_word = new javax.swing.JPasswordField();
        jPanel2 = new javax.swing.JPanel();
        btnNextAll = new javax.swing.JButton();
        btnPrev = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbldanh_sach = new javax.swing.JTable();
        txtRows = new javax.swing.JTextField();
        btnNext = new javax.swing.JButton();
        btnPrevAll = new javax.swing.JButton();
        btnCanCel = new javax.swing.JButton();

        btnedit.setText("Sửa");
        btnedit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btneditActionPerformed(evt);
            }
        });

        btnsave.setText("Lưu");
        btnsave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsaveActionPerformed(evt);
            }
        });

        btnadd.setText("Thêm mới");
        btnadd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnaddActionPerformed(evt);
            }
        });

        btnclose.setText("Thoát");
        btnclose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btncloseActionPerformed(evt);
            }
        });

        btndelete.setText("Xóa");
        btndelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btndeleteActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(102, 102, 255));
        jLabel7.setText("QUẢN LÝ NGƯỜI DÙNG");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin người dùng"));

        jLabel5.setText("Xác nhận mật khẩu:");

        jLabel3.setText("Mật khẩu:");

        jLabel4.setText("Tên nhóm:");

        cbbgroup_id.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        chkactive.setText("Active");

        jLabel1.setText("Họ tên:");

        jLabel2.setText("Tài khoản:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txthoten, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtuser_name, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbbgroup_id, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(43, 43, 43)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel5))
                        .addGap(40, 40, 40)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtpass_word, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtre_pass_word, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(chkactive, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(49, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtuser_name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(9, 9, 9)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txthoten, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtpass_word, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(6, 6, 6)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtre_pass_word, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(chkactive)
                    .addComponent(cbbgroup_id, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh sách người dùng"));

        btnNextAll.setText(">>");
        btnNextAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextAllActionPerformed(evt);
            }
        });

        btnPrev.setText("<");
        btnPrev.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrevActionPerformed(evt);
            }
        });

        tbldanh_sach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbldanh_sach.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbldanh_sachMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbldanh_sach);

        btnNext.setText(">");
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });

        btnPrevAll.setText("<<");
        btnPrevAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrevAllActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(btnPrevAll)
                        .addGap(18, 18, 18)
                        .addComponent(btnPrev)
                        .addGap(18, 18, 18)
                        .addComponent(txtRows, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnNext)
                        .addGap(18, 18, 18)
                        .addComponent(btnNextAll))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 645, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)
                .addGap(5, 5, 5)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnPrevAll)
                    .addComponent(btnPrev)
                    .addComponent(btnNext)
                    .addComponent(btnNextAll)
                    .addComponent(txtRows, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        btnCanCel.setText("Huỷ");
        btnCanCel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCanCelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(jLabel7)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnadd)
                                .addGap(7, 7, 7)
                                .addComponent(btnedit)
                                .addGap(7, 7, 7)
                                .addComponent(btnsave)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnCanCel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btndelete)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnclose)))))
                .addGap(26, 26, 26))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jLabel7)
                .addGap(21, 21, 21)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnedit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnsave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnclose, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnadd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btndelete, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnCanCel))
                .addGap(23, 23, 23))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btnaddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnaddActionPerformed
        // TODO add your handling code here:
        this.add = true;
        this.clearText();
        this.edit = false;
        this.setEditable(true);
        this.isSetEnabled(false);
        this.cbbgroup_id.setEnabled(true);
        this.cbbgroup_id.setSelectedIndex(-1);
    }//GEN-LAST:event_btnaddActionPerformed

    private void isSetEnabled(Boolean b){
        Boolean flag = b == true ? false : true;
        this.btnedit.setEnabled(b);
        this.btnsave.setEnabled(flag);
        this.btndelete.setEnabled(b);
        this.btnadd.setEnabled(b);
        this.btnCanCel.setEnabled(flag);
    }
    
    private void btneditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btneditActionPerformed
        // TODO add your handling code here:
        this.add = false;        
        this.edit = true;
        this.setEditable(true);
        this.txtuser_name.setEditable(false);
        this.btnadd.setEnabled(false);
        this.btnedit.setEnabled(false);
        this.btnsave.setEnabled(true);
        this.btndelete.setEnabled(false);
        this.btndelete.setEnabled(false);
        this.cbbgroup_id.setEnabled(true);
    }//GEN-LAST:event_btneditActionPerformed

    private void btnsaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsaveActionPerformed
        // TODO add your handling code here:
        
        Boolean flag = false;
        if(this.add){
            flag = this.addNew();
        }
        else if(this.edit){
            flag = editRow();
        }
        if(flag){
            this.btnedit.setEnabled(true);
            this.btnadd.setEnabled(true);
            this.btnsave.setEnabled(false);
            this.btnCanCel.setEnabled(false);
            this.btndelete.setEnabled(true);            
            if(this.add){
                FrmNhanVien  f = new FrmNhanVien(userId,ho_tennv);
                f.setVisible(true);
                this.setVisible(false);
            }
            this.add = false;
            this.edit = false;
        }
    }//GEN-LAST:event_btnsaveActionPerformed

    private void btndeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btndeleteActionPerformed
        // TODO add your handling code here:
        int reply = JOptionPane.showConfirmDialog(this, "Bạn có muốn xóa user này không ?");
        if(reply == JOptionPane.YES_OPTION){            
            if(currentUser != null){
                CModel _model = new CModel(QuanLySieuThi.user_name);
                _model.deleteUser(currentUser);
                _model.saveHisTory();
                _model.Close();
                loadDataUsers();
            }
        }
    }//GEN-LAST:event_btndeleteActionPerformed

    private void btncloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btncloseActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_btncloseActionPerformed

    private void tbldanh_sachMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbldanh_sachMouseClicked
        // TODO add your handling code here:
        JTable table =(JTable)evt.getSource();
        Point p = evt.getPoint();
        int rowIndex = table.rowAtPoint(p);            
        if(rowIndex >=0){
            table.setRowSelectionInterval(rowIndex, rowIndex);
            this.currentIndex = rowIndex;
            this.view(rowIndex);
        }
    }//GEN-LAST:event_tbldanh_sachMouseClicked

    private void btnPrevActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrevActionPerformed
        // TODO add your handling code here:
        if(this.currentIndex > 0){
            this.currentIndex -= 1;
            this.view(this.currentIndex);
        }
    }//GEN-LAST:event_btnPrevActionPerformed

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
        // TODO add your handling code here:
        if(this.currentIndex < this.numberRow){
            this.currentIndex += 1;
            this.view(this.currentIndex);
        }
    }//GEN-LAST:event_btnNextActionPerformed

    private void btnPrevAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrevAllActionPerformed
        // TODO add your handling code here:
        this.currentIndex = 0;
        this.view(this.currentIndex);
    }//GEN-LAST:event_btnPrevAllActionPerformed

    private void btnNextAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextAllActionPerformed
        // TODO add your handling code here:
         this.currentIndex = this.numberRow - 1;
        this.view(this.currentIndex);
    }//GEN-LAST:event_btnNextAllActionPerformed

    private void btnCanCelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCanCelActionPerformed
        // TODO add your handling code here:
        this.loadDataUsers();
        this.add = false;
        this.edit = false;
    }//GEN-LAST:event_btnCanCelActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmUser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmUser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmUser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmUser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrmUser().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCanCel;
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnNextAll;
    private javax.swing.JButton btnPrev;
    private javax.swing.JButton btnPrevAll;
    private javax.swing.JButton btnadd;
    private javax.swing.JButton btnclose;
    private javax.swing.JButton btndelete;
    private javax.swing.JButton btnedit;
    private javax.swing.JButton btnsave;
    private javax.swing.JComboBox cbbgroup_id;
    private javax.swing.JCheckBox chkactive;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbldanh_sach;
    private javax.swing.JTextField txtRows;
    private javax.swing.JTextField txthoten;
    private javax.swing.JPasswordField txtpass_word;
    private javax.swing.JPasswordField txtre_pass_word;
    private javax.swing.JTextField txtuser_name;
    // End of variables declaration//GEN-END:variables
}
